/*
* @author : TF-BJ-C064
* @creation : 2014-8-21 下午12:33:19
* @description : 
*
*/

package com.action.park;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.http.client.ClientProtocolException;
import org.apache.log4j.Logger;

import com.base.BaseAction;
import com.tool.DateTimeTools;
import com.umeng.App;
import com.umeng.UMLoginAction;
import com.umeng.UMStat;
import com.umeng.UMUser;
import com.util.GUtil;

/**
 * 活跃用户统计
 * <p>Title: StatActiveUserAction</p>
 * <p>Description:
 * 		抓取友盟的活跃用户数据，写入数据库，然后在WEB展示
 *  </p>
 * <p>Company: www.teleframe.cn</p> 
 * @author	lijian
 * @date	2014-8-21
 */
public class StatActiveUserAction extends BaseAction {

	private static final long serialVersionUID = 5639757141368842816L;
	
	public Logger log = Logger.getLogger(StatActiveUserAction.class);
	
	private static String USER_NAME = "app@teleframe.cn";
	private static String PASSWORD = "teleframe2014";

	private UMUser umuser = new UMUser(USER_NAME, PASSWORD);
	private UMLoginAction umLoginAction = new UMLoginAction();
	private UMStat stat = new UMStat();
	
	private String startdate;
	private String enddate;
	private String month;
	private List<App> list ;
	
	SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");

	@Override
	public String handle() throws Exception {
		this.getapps();
		list = stat.getApplist();
		session.put("list", list);
		
		Timestamp inTime = new Timestamp(System.currentTimeMillis());
		Timestamp outStartTime = new Timestamp(System.currentTimeMillis());
		Timestamp outEndTime = new Timestamp(System.currentTimeMillis());
		
		System.out.println("----------this month----");
		DateTimeTools.getMonthDays(inTime, outStartTime, outEndTime);
		startdate = sdf.format(outStartTime);
		enddate = sdf.format(outEndTime);
		
		return GUtil.SUCCESS;
	}

	public void getapps() throws ClientProtocolException, IOException, KeyManagementException, NoSuchAlgorithmException{
		
		UMUser tempuser = (UMUser)session.get("umuser");
		if(tempuser==null){
			umLoginAction.setUser(umuser);
			stat.setUser(umuser);
			umLoginAction.index();
			umLoginAction.login();
		}else{
			umuser = tempuser;
			umLoginAction.setUser(umuser);
			stat.setUser(umuser);
		}
		
		stat.apps();
		session.put("umuser", umuser);
	}
	@Override
	public String query() throws Exception {
		
		this.getapps();
		list = (List<App>) session.get("list");
		if(list==null)
			list = stat.getApplist();

		JSONObject json = new JSONObject();
		JSONArray jsonarray = new JSONArray();
		for(App app: stat.getApplist()){
			JSONObject jstat = stat.statActive(startdate, enddate, app);
			if(jstat!=null){
				app.setJsonstat(jstat);
				jsonarray.add(jstat);
			}
		}
		if(jsonarray.size()>0){
			json.put("error", 0);
			json.put("data", stat.getApplist());
		}else{
			json.put("error", -1);
			json.put("msg", "暂无数据");
		}
		
		PrintWriter out = response.getWriter();
		out.print(json.toString());
		out.close();
		
		return GUtil.SUCCESS;
	}

	@Override
	public String add() throws Exception {
		return null;
	}

	@Override
	public String update() throws Exception {
		return null;
	}

	@Override
	public String delete() throws Exception {
		return null;
	}

	@Override
	public String view() throws Exception {
		return null;
	}

	@Override
	public boolean validate(Object obj) throws Exception {
		return false;
	}

	public String getStartdate() {
		return startdate;
	}

	public void setStartdate(String startdate) {
		this.startdate = startdate;
	}

	public String getEnddate() {
		return enddate;
	}

	public void setEnddate(String enddate) {
		this.enddate = enddate;
	}

	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public List<App> getList() {
		return list;
	}

	public void setList(List<App> list) {
		this.list = list;
	}

}
